package com.bookmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bookmall.dao.CartDao;
import com.bookmall.entity.Cart;
import com.bookmall.util.DBUtil;

public class CartDaoImpl implements CartDao{
	
	@Override
	public boolean insert(Cart t) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "insert into cart_tb(bookid,userid,cartnum,create_time) values(?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		
		ps.setInt(1, t.getBookId());
		ps.setInt(2, t.getUserId());
		ps.setInt(3, t.getCartNum());
		ps.setObject(4, t.getCreateTime());
		
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean update(Cart t) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "update cart_tb set bookid = ?,userid = ?,cartnum = ?,create_time = ? where cartid = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		
		ps.setInt(1, t.getBookId());
		ps.setInt(2, t.getUserId());
		ps.setInt(3, t.getCartNum());
		ps.setObject(4, t.getCreateTime());
		ps.setInt(5, t.getCartId());
		
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "update cart_tb set delete_flag = 1 where cartid= ? and delete_flag = 0";
		PreparedStatement ps = c.prepareStatement(sql);
		
		ps.setInt(1, id);
		
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public Cart selectById(int id) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "select cartid,bookid,userid,cartnum,create_time,modify_time from cart_tb where cartid = ? and delete_flag = 0";
		PreparedStatement ps = c.prepareStatement(sql);
		
		ps.setInt(1, id);
		
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			Cart cart = new Cart();
			cart.setCartId(rs.getInt(1));
			cart.setBookId(rs.getInt(2));
			cart.setUserId(rs.getInt(3));
			cart.setCartNum(rs.getInt(4));
			cart.setCreateTime(rs.getTimestamp(5).toLocalDateTime());
			if (rs.getTimestamp(6) != null) {
				cart.setModifyTime(rs.getTimestamp(6).toLocalDateTime());
			}
			DBUtil.closeConnection(c, ps, rs);
			return cart;
		} else {
			DBUtil.closeConnection(c, ps, rs);
			return null;
		}
	}

	@Override
	public List<Cart> selectByPage(int page, int pageSize) throws SQLException {
		Connection c = DBUtil.getConnection();
		List<Cart> carts = new ArrayList<>();
		String sql = "select cartid,bookid,userid,cartnum,create_time,modify_time from cart_tb where delete_flag=0 limit ?,?";
		PreparedStatement ps = c.prepareStatement(sql);
		
		ps.setInt(1, (page - 1) * pageSize);
		ps.setInt(2, pageSize);
		
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			Cart cart = new Cart();
			cart.setCartId(rs.getInt(1));
			cart.setBookId(rs.getInt(2));
			cart.setUserId(rs.getInt(3));
			cart.setCartNum(rs.getInt(4));
			cart.setCreateTime(rs.getTimestamp(5).toLocalDateTime());
			if (rs.getTimestamp(6) != null) {
				cart.setModifyTime(rs.getTimestamp(6).toLocalDateTime());
			}
			carts.add(cart);
		}
		
		DBUtil.closeConnection(c, ps, rs);
		return carts.isEmpty() ? Collections.emptyList() : carts;
	}
}
